package cc.comac.mom.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.QarRestrictedPartService;
import cc.comac.mom.api.lifelimit.LifelimitPartInstanceService;
import cc.comac.mom.bean.QarRestrictedPart;
import cc.comac.mom.bean.lifelimit.GenerateInstanceDTO;
import cc.comac.mom.bean.lifelimit.LifeLimitPrintResultBean;
import cc.comac.mom.datahandlers.entity.QarRestrictedPartDataHandler;
import cc.comac.mom.datahandlers.entity.QualityQarCheckInvoiceDataHandler;
import cc.comac.mom.datahandlers.entity.QualitySapPurchaseOrderDataHandler;
import cc.comac.mom.entity.QarRestrictedPartEntity;
import cc.comac.mom.entity.QarRestrictedPartItemEntity;
import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import cc.comac.mom.entity.print.RestrictedPartItemPrint;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;
@RestController
@Service
public class QarRestrictedPartServiceImpl implements QarRestrictedPartService{

	@Autowired
	private QarRestrictedPartDataHandler qarRestrictedPartDataHandler;
	@Autowired
	private LifelimitPartInstanceService lifelimitPartInstanceService;
	@Autowired
	private QualityQarCheckInvoiceDataHandler qualityQarCheckInvoiceDataHandler;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Override
	public ResultBean updateCheckData(@RequestBody List<QarRestrictedPart> list) {
		ResultBean resultBean2 = new ResultBean(true);
		QualityQarCheckInvoiceEntity selectByQarNumber = qualityQarCheckInvoiceDataHandler.selectByQarNumber(list.get(0).getInnerData().getQarNo());
		List<GenerateInstanceDTO> itemList = new ArrayList<GenerateInstanceDTO>();
		for (QarRestrictedPart qarRestrictedPart : list) {
			QarRestrictedPartEntity innerData = qarRestrictedPart.getInnerData();
			List<QarRestrictedPartItemEntity> restrictedPartItemList = qarRestrictedPart.getRestrictedPartItemList();
			if (!CollectionUtils.isEmpty(restrictedPartItemList)) {
				for (QarRestrictedPartItemEntity itemEntity : restrictedPartItemList) {
					GenerateInstanceDTO generateInstanceDTO = new GenerateInstanceDTO();
					generateInstanceDTO.setBaseDate(innerData.getBaseDate());
					generateInstanceDTO.setBatch(innerData.getRestrictedSerialNumber());
					generateInstanceDTO.setDeliveryDate(null);// 交付日期
					generateInstanceDTO.setEndBatch(innerData.getFinishedProductSerialNumber());
					generateInstanceDTO.setEndProductCode(innerData.getFinishedProductNo());
					generateInstanceDTO.setEndSerialNum(innerData.getFinishedProductSerialNumber());
					generateInstanceDTO.setInspect(baseEntityDataUtil.getCurrentUser());
					generateInstanceDTO.setInspectDate(TimeUtil.fomatDate(TimeUtil.getLocalDate()));
					generateInstanceDTO.setInventoryDeadline(itemEntity.getStorageTime());
					generateInstanceDTO.setMaintainDate(itemEntity.getBaseTime());
					generateInstanceDTO.setManufactureDate(null); // 生产日期？
					generateInstanceDTO.setPartCode(selectByQarNumber.getPartNo());
					generateInstanceDTO.setPartName(selectByQarNumber.getPartName());
					generateInstanceDTO.setQarNo(selectByQarNumber.getQarNo());
					generateInstanceDTO.setSerialNum(innerData.getRestrictedSerialNumber());
					itemList.add(generateInstanceDTO);
				}
			}
		}
		
		if (itemList.size() >0) {
			LifeLimitPrintResultBean insertLifelimitPartInstance = lifelimitPartInstanceService.insertLifelimitPartInstance(itemList);
			if (insertLifelimitPartInstance.getStatus()==0) {
				resultBean2.setErrorMessge(insertLifelimitPartInstance.getMsg());
				return resultBean2;
			}
		}
		ResultBean resultBean = qarRestrictedPartDataHandler.updateCheckData(list);
		return resultBean;
	}
	@Override
	public ResultBean batchInsertCheckGetData(@RequestBody QarRestrictedPartEntity entity) {
		ResultBean batchInsertCheckData = qarRestrictedPartDataHandler.batchInsertCheckData(entity);
		return batchInsertCheckData;
	}
	@Override
	public RestrictedPartItemPrint selectPrintData(@RequestBody Map<String,String> map) {
		String string = map.get("itemId");
		long itemId = Long.parseLong(string);
		String partNo = map.get("partNo");
		RestrictedPartItemPrint selectPrintData = qarRestrictedPartDataHandler.selectPrintData(itemId, partNo);
		return selectPrintData;
	}

}
